package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {
@Autowired
private UserInfoMapper userInfoMapper;
    @Override
    public void save(UserInfo userInfo) {
          userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userID) {
        return userInfoMapper.selectById(userID);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, Integer age, String gender) {
        LambdaQueryWrapper<UserInfo> lqw=new LambdaQueryWrapper<>();
        if(age!=null) {
            lqw.le(UserInfo::getAge,age);
        }
        if(gender!=null) {
            lqw.eq(UserInfo::getGender, gender);
        }
        if(ids.size()!=0) {
            lqw.in(UserInfo::getId, ids);
            List<UserInfo> list = userInfoMapper.selectList(lqw);
            //用工具类得到map集合
            Map<Long, UserInfo> userInfoMap = CollUtil.fieldValueMap(list, "id");
            return userInfoMap;
        }
        return null;

    }

    @Override
    public PageResult findByUserInfo(Integer page, Integer pagesize, String keyword, List<Long> ids) {
        LambdaQueryWrapper<UserInfo> lqw=new LambdaQueryWrapper<>();
        if(keyword!=null) {
      lqw.like(UserInfo::getNickname,keyword);
        }
        if(ids.size()!=0) {
            lqw.in(UserInfo::getId, ids);
            List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
            List<ContactVo> contactVos = new ArrayList<>();
            for (UserInfo userInfo : userInfos) {
                if (userInfo != null) {
                    ContactVo init = ContactVo.init(userInfo);
                    contactVos.add(init);
                }
            }
            return new PageResult(page, pagesize, 0, contactVos);
        }
        return null;
    }

    @Override
    public List<UserInfo> findAll(Integer page, Integer pagesize) {
        Page<UserInfo> page1 =new Page<>(page,pagesize);
        LambdaQueryWrapper<UserInfo> lqw =new LambdaQueryWrapper<>();
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(page1, lqw);
        List<UserInfo> records = userInfoPage.getRecords();
        return records;
    }

    @Override//查总树
    public Integer findCounts(Integer page, Integer pagesize) {
        LambdaQueryWrapper<UserInfo> lqw =new LambdaQueryWrapper<>();
        Integer integer = userInfoMapper.selectCount(lqw);
        return integer;

    }
}

//    @Override
//    public PageResult findByUserInfo(Integer page, Integer pagesize, String keyword) {
//        LambdaQueryWrapper<UserInfo> lqw=new LambdaQueryWrapper<>();
//        lqw.like(UserInfo::getNickname,keyword);
//        List<UserInfo> userInfos = userInfoMapper.selectList(lqw);
//        List<ContactVo> contactVos =new ArrayList<>();
//        for (UserInfo userInfo : userInfos) {
//            if(userInfo!=null) {
//                ContactVo init = ContactVo.init(userInfo);
//                contactVos.add(init);
//            }
//        }
//        return new PageResult(page,pagesize,0,contactVos);
//    }

